#ifndef MESSAGE_H
#define MESSAGE_H

#include "stdafx.h"
#include <vector>
#include <sstream>
#include "../Core/DateTime.h"
#include "../Shared/MessageAttribute.h"
#include "../Shared/MessageStringAttribute.h"
#include "../Shared/MessageNumericAttribute.h"

using namespace std;

class Message
{
    public:
        Message() {
            ServiceId.Value = 0;

            mAttributes.push_back(&ServiceId);
            mAttributes.push_back(&Length);
            //mAttributes.insert(make_pair("Newline" , NULL));
            mAttributes.push_back(&UserAgent);
            mAttributes.push_back(&UserToken);
            mAttributes.push_back(&TargetId);
        }

        virtual ~Message() {
        }

        virtual void ParseMessage(std::string pMessage) {
            mRawMessage = pMessage;
            istringstream stream(pMessage);

            for (vector<MessageAttribute*>::iterator it = mAttributes.begin() ; it != mAttributes.end() ; it++) {
                if (stream.eof()) { ///Supposedly not eof here, right?
                    throw exception();
                }

                if (*it == NULL) {

                }
                else {
                    (*it)->ReadValue(stream);
                }
            }
        }

        unsigned int DispatcherId;
        MessageNumericAttribute ServiceId;
        MessageNumericAttribute Length;
        DateTime TimeStamp;

        MessageStringAttribute UserAgent;
        MessageStringAttribute UserToken;
        MessageStringAttribute TargetId;

        virtual std::string ToString() {
            ostringstream stream;
            for (vector<MessageAttribute*>::iterator it = mAttributes.begin() ; it != mAttributes.end() ; it++) {
                (*it)->WriteValue(stream);
            }
            return stream.str();
        }

    protected:
        std::vector<MessageAttribute*> mAttributes;

    private:
        std::string mRawMessage;

};

#endif // MESSAGE_H
